package com.dashu.core.service;

import com.dashu.base.bean.excel.ExcelTeacher;
import com.dashu.base.enums.ReplRyCode;
import com.dashu.base.enums.common.GenderType;
import com.dashu.base.enums.common.Status;
import com.dashu.base.enums.device.Iden;
import com.dashu.base.enums.device.PicType;
import com.dashu.base.enums.school.LessonType;
import com.dashu.base.enums.school.TeacherType;
import com.dashu.base.utils.RegularUtils;
import com.dashu.base.utils.UIDUtil;
import com.dashu.base.utils.VerificationUtil;
import com.dashu.base.vo.DevicePicAddVO;
import com.dashu.base.vo.DevicePicDelReqVO;
import com.dashu.base.vo.DevicePicUpdReqVO;
import com.dashu.core.dto.company.*;
import com.dashu.core.model.*;
import com.dashu.core.result.PageEnty;
import com.dashu.base.utils.CopyUtil;
import com.dashu.core.handler.VerificationException;
import com.dashu.core.vo.TeacherClsVO;
import com.dashu.core.vo.company.CompanyTeacherListVO;
import com.dashu.core.vo.company.CompanyTeacherVO;
import com.dashu.core.vo.UpdateEntityVO;
import com.dashu.core.vo.company.UpCompanyTeacherVO;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class CompanyTeacherService extends DataService {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private CompanyClsService companyClsService;
    @Autowired
    private CompanyDepartmentService companyDepartmentService;
    @Autowired
    private TeacherClsService teacherClsService;
    @Autowired
    private DeviceService deviceService;

    @Transactional
    public CompanyTeacherDTO add(CompanyTeacherVO companyTeacherVO) {
        CompanyTeacher companyTeacher = CopyUtil.copyEnty(companyTeacherVO, CompanyTeacher.class);
        boolean isPhone = findByTeacherPhone(companyTeacherVO.getTeacherPhone());
        if (!isPhone)
            throw new VerificationException(ReplRyCode.EXIST_PHONE, ReplRyCode.EXIST_PHONE.getMessage());
        try {
            long userId = Long.parseLong(VerificationUtil.getUserId());
            companyTeacher.setId(UIDUtil.nextId());
            companyTeacher.setCreateUser(userId);
            companyTeacher.setCreateTime(System.currentTimeMillis());
            companyTeacher.setTeacherType(TeacherType.map.get(companyTeacherVO.getTeacherType()));
            if (!StringUtils.isEmpty(companyTeacherVO.getIcon())) {
                companyTeacher.setStatus(Status.INITIALIZATION);
            } else {
                companyTeacher.setStatus(Status.UNINITIALIZED);
            }
            companyTeacher.setDepartmentId(companyTeacherVO.getCompanyDepartmentId());
            TeacherClsVO teacherClsVO = new TeacherClsVO();
            teacherClsVO.setBindCls(companyTeacherVO.getCompanyClsId());
            teacherClsVO.setBindTeacher(companyTeacher.getId());
            teacherClsVO.setCompanyId(companyTeacherVO.getCompanyId());
            teacherClsVO.setIdentity(companyTeacherVO.getIdentity());
            teacherClsVO.setCompanyDepartmentId(companyTeacherVO.getCompanyDepartmentId());
            teacherClsService.add(teacherClsVO);
            entityManager.persist(companyTeacher);
//            entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);

        } catch (Exception e) {
            e.printStackTrace();
            throw new VerificationException(ReplRyCode.DATA_NOT_INVALID, ReplRyCode.DATA_NOT_INVALID.getMessage());
        }
        CompanyTeacherDTO companyTeacherDTO = CopyUtil.copyEnty(companyTeacher, CompanyTeacherDTO.class);
        return companyTeacherDTO;

    }

    @Transactional
    public List<CompanyTeacherDTO> batchAdd(List<ExcelTeacher> excelTeachers, long companyId) {
        CompanyTeacherVO companyTeacherVO = new CompanyTeacherVO();
        companyTeacherVO.setCompanyId(companyId);
        List<CompanyTeacherDTO> companyTeacherDTO = Lists.newArrayList();
        excelTeachers.stream().forEach(et -> {
            companyTeacherVO.setTeacherName(et.getName());
            companyTeacherVO.setGender(et.getGender().name());
            if (et.getPhone().length() != 11) {
                throw new VerificationException(ReplRyCode.ERROR_PHONE, ReplRyCode.ERROR_PHONE.getMessage());
            }
            companyTeacherVO.setTeacherPhone(Long.parseLong(et.getPhone()));
            companyTeacherDTO.add(add(companyTeacherVO));
        });

        return companyTeacherDTO;

    }

    @Transactional
    public boolean deleteByCompanyTeacherId(long companyTeacherId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.eq(companyTeacherId));
        List<TeacherCls> teacherCls = teacherClsService.teacherClsList(companyTeacherId, 0);
        teacherCls.stream().forEach(tc -> {
            teacherClsService.deleteByTeacherClsId(tc.getId());
        });
        CompanyTeacher CompanyTeacher;
        queryFactory.select();
        CompanyTeacher = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue()).fetchOne();
        if (CompanyTeacher == null) return false;
        deleteImgTeacher(CompanyTeacher.getId());
        entityManager.remove(CompanyTeacher);
//            entityManager.flush();
        entityManager.setFlushMode(FlushModeType.AUTO);
        return true;

    }


    @Transactional
    public boolean deleteBatchTeacher(List<Long> companyTeacherIds) {
        companyTeacherIds.stream().forEach(ct -> {
            if (!deleteByCompanyTeacherId(ct))
                throw new VerificationException(ReplRyCode.BATCH_REMOV_ERROR, ReplRyCode.BATCH_REMOV_ERROR.getMessage());
        });
        return true;

    }
    public List<CompanyTeacher> findByCompanyAll(List<Long> companys) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.companyId.in(companys));
        List<CompanyTeacher> companyTeachers = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue())
                .fetch();
        return companyTeachers;

    }


    @Transactional
    public boolean deleteImgTeacher(long companyTeacherId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.eq(companyTeacherId));
        CompanyTeacher companyTeacher =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue())
                        .fetchOne();
        DevicePicDelReqVO devicePicDelReqVO = new DevicePicDelReqVO();
        devicePicDelReqVO.setUserId(String.valueOf(companyTeacher.getId()));
        devicePicDelReqVO.setUserName(companyTeacher.getTeacherName());
        devicePicDelReqVO.setIden(Iden.TEACHER);
        devicePicDelReqVO.setResp(companyTeacher.getIcon());
        deviceService.DeldeviceBaseHand(companyTeacher.getCompanyId(), devicePicDelReqVO);

        return true;

    }

    @Transactional
    public boolean updateImgTeacher(long companyTeacherId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.eq(companyTeacherId));
        CompanyTeacher companyTeacher =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue())
                        .fetchOne();
        DevicePicUpdReqVO devicePicUpdReqVO = new DevicePicUpdReqVO();
        devicePicUpdReqVO.setUserId(String.valueOf(companyTeacher.getId()));
        devicePicUpdReqVO.setUserName(companyTeacher.getTeacherName());
        devicePicUpdReqVO.setIden(Iden.TEACHER);
        devicePicUpdReqVO.setPicType(PicType.URL);
        devicePicUpdReqVO.setResp(companyTeacher.getIcon());
        deviceService.UpdeviceBaseHand(companyTeacher.getCompanyId(), devicePicUpdReqVO);
        return true;

    }

    @Transactional
    public boolean issueBatchTeacher(List<Long> companyTeacherIds) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.in(companyTeacherIds));
        List<CompanyTeacher> companyTeachers =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue())
                        .fetch();

        companyTeachers.stream().forEach(ct -> {
            DevicePicAddVO devicePicAddVO = new DevicePicAddVO();
            devicePicAddVO.setUserId(Iden.TEACHER + "_" + String.valueOf(ct.getId()));
            devicePicAddVO.setUserName(ct.getTeacherName());
            devicePicAddVO.setIden(Iden.TEACHER);
            devicePicAddVO.setPicType(PicType.URL);
            devicePicAddVO.setResp(ct.getIcon());
            ct.setStatus(Status.UNDERREVIEW);
            entityManager.merge(ct);
            entityManager.setFlushMode(FlushModeType.AUTO);
            deviceService.AdddeviceBaseHand(ct.getCompanyId(), devicePicAddVO);
        });

        return true;

    }

    @Transactional
    public CompanyTeacherDTO updataByCompanyTeacherId(UpdateEntityVO<UpCompanyTeacherVO> updateCompanyTeacherVO) throws Exception {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        long userId = Long.parseLong(VerificationUtil.getUserId());
        UpCompanyTeacherVO upCompanyTeacherVO = updateCompanyTeacherVO.getEntityVO();
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.eq(updateCompanyTeacherVO.getId()));
        CompanyTeacher companyTeacher =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue()).fetchOne();
        if (companyTeacher == null)
            throw new VerificationException(ReplRyCode.COMPANY_TEACHER_NOT_DATA, ReplRyCode.COMPANY_TEACHER_NOT_DATA.getMessage());
        companyTeacher.setUpdateTime(new Date().getTime());
        companyTeacher.setUpdateUser(userId);
        if (upCompanyTeacherVO.getTeacherPhone() != 0)
            companyTeacher.setTeacherPhone(upCompanyTeacherVO.getTeacherPhone());
        if (!StringUtils.isEmpty(upCompanyTeacherVO.getTeacherName()))
            companyTeacher.setTeacherName(upCompanyTeacherVO.getTeacherName());
        if (!StringUtils.isEmpty(upCompanyTeacherVO.getEducation()))
            companyTeacher.setEducation(LessonType.map.get(upCompanyTeacherVO.getEducation()));
        if (!StringUtils.isEmpty(upCompanyTeacherVO.getGender()))
            companyTeacher.setGender(GenderType.map.get(upCompanyTeacherVO.getGender()));
        if (!StringUtils.isEmpty(upCompanyTeacherVO.getIcon())) {
            companyTeacher.setIcon(upCompanyTeacherVO.getIcon());
            companyTeacher.setStatus(Status.INITIALIZATION);
            updateImgTeacher(companyTeacher.getId());
        }
        companyTeacher = entityManager.merge(companyTeacher);
//        entityManager.flush();
        entityManager.setFlushMode(FlushModeType.AUTO);
        CompanyTeacherDTO CompanyTeacherDTO = CopyUtil.copyEnty(companyTeacher, CompanyTeacherDTO.class);
        return CompanyTeacherDTO;

    }


    @Transactional
    public void updataAttendanceCompany(long companyId, long attendanceId, TeacherType teacherTypes) throws Exception {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        long userId = Long.parseLong(VerificationUtil.getUserId());
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.companyId.eq(companyId));
        booleanBuilder.and(qCompanyTeacher.teacherType.in(teacherTypes));
        List<CompanyTeacher> companyTeachers =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue())
                        .fetch();
        companyTeachers.stream().forEach(ct -> {
            ct.setUpdateUser(userId);
            ct.setUpdateTime(new Date().getTime());
            ct.setAttendanceId(attendanceId);
            entityManager.merge(ct);
            //entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        });
    }

    @Transactional
    public void updataGateCompany(long companyId, long gateId, TeacherType teacherTypes) throws Exception {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        long userId = Long.parseLong(VerificationUtil.getUserId());
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.companyId.eq(companyId));
        booleanBuilder.and(qCompanyTeacher.teacherType.in(teacherTypes));
        List<CompanyTeacher> companyTeachers =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue())
                        .fetch();
        companyTeachers.stream().forEach(ct -> {
            ct.setUpdateUser(userId);
            ct.setUpdateTime(new Date().getTime());
            ct.setGateId(gateId);
            entityManager.merge(ct);
            //entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        });
    }


    @Transactional
    public void updataAttendance(long teacherId, long attendanceId) throws Exception {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        long userId = Long.parseLong(VerificationUtil.getUserId());
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.eq(teacherId));
        CompanyTeacher companyTeachers =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue())
                        .fetchOne();

        companyTeachers.setUpdateUser(userId);
        companyTeachers.setUpdateTime(new Date().getTime());
        companyTeachers.setAttendanceId(attendanceId);
        entityManager.merge(companyTeachers);
        //entityManager.flush();
        entityManager.setFlushMode(FlushModeType.AUTO);
    }

    @Transactional
    public void updataGate(long teacherId, long gateId) throws Exception {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        long userId = Long.parseLong(VerificationUtil.getUserId());
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.companyId.eq(teacherId));
        CompanyTeacher companyTeachers =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue())
                        .fetchOne();

        companyTeachers.setUpdateUser(userId);
        companyTeachers.setUpdateTime(new Date().getTime());
        companyTeachers.setGateId(gateId);
        entityManager.merge(companyTeachers);
        //entityManager.flush();
        entityManager.setFlushMode(FlushModeType.AUTO);
    }


    @Transactional
    public void updataAttendanceDepartment(long department, long attendanceId, List<TeacherType> teacherTypes) throws Exception {
        List<CompanyCls> teacherCls = companyClsService.findByDepartmentId(department);
        teacherCls.stream().forEach(tc -> {
//            updataAttendanceCls
        });
    }


    public List<CompanyTeacherDTO> findByCreteUser(PageEnty<Long> baseVo) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.createUser.eq(baseVo.getBaseId()));
        List<CompanyTeacher> companyTeachers = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue())
                .fetch();
        List<CompanyTeacherDTO> recycleDTOS = CopyUtil.copyList(companyTeachers, CompanyTeacherDTO.class);
        return recycleDTOS;

    }

    public CompanyTeacherDTO findByCompanyTeacherId(long companyTeacherId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.eq(companyTeacherId));
        CompanyTeacher companyTeacher = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue()).fetchOne();
        if (companyTeacher == null)
            throw new VerificationException(ReplRyCode.COMPANY_TEACHER_NOT_DATA, ReplRyCode.COMPANY_TEACHER_NOT_DATA.getMessage());
        CompanyTeacherDTO CompanyTeacherDTO = CopyUtil.copyEnty(companyTeacher, CompanyTeacherDTO.class);
        return CompanyTeacherDTO;
    }

    public boolean findByTeacherPhone(long teacherPhone) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.teacherPhone.eq(teacherPhone));
        CompanyTeacher companyTeacher = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue()).fetchOne();
        if (companyTeacher == null)
            return true;
        return false;
    }


    public List<CompanyTeacher> findByUserId(long userId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        List<Long> companieIds = Lists.newArrayList();
        List<Company> companies = companyService.findByBaseId(userId);
        companies.stream().forEach(c -> {
            companieIds.add(c.getId());
        });
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.companyId.in(companieIds));
        List<CompanyTeacher> companyTeachers = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue())
                .fetch();
        return companyTeachers;

    }


    public void deleteCompany(long companyId) {
        List<CompanyTeacher> companyTeachers = findByCompanieId(companyId);
        companyTeachers.stream().forEach(ct -> {
            ct.setCompanyId(0);
            entityManager.merge(ct);
//        entityManager.flush();
            entityManager.setFlushMode(FlushModeType.AUTO);
        });
    }


    public List<CompanyTeacher> findByCompanieId(long companyId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.companyId.eq(companyId));
        List<CompanyTeacher> companyTeachers = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue())
                .fetch();
        return companyTeachers;

    }

    public long countCompanyId(long companyId) throws Exception {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.companyId.eq(companyId));
        long count = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue())
                .fetchCount();
        return count;
    }


    public List<CompanyTeacher> findByCompanyDepartmentId(long companieDepartmentId) {
        List<CompanyTeacher> companyTeacherAll = Lists.newArrayList();
        List<CompanyCls> companyCls = companyClsService.findByDepartmentId(companieDepartmentId);
        companyCls.stream().forEach(cc -> {
            List<CompanyTeacher> companyTeachers = findByCompanieClsId(cc.getId());
            companyTeacherAll.addAll(companyTeachers);
        });
        companyTeacherAll.sort(Comparator.comparing(CompanyTeacher -> CompanyTeacher.getId()));
        return companyTeacherAll;

    }

    public List<CompanyTeacher> findByCompanieClsId(long companieClsId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        List<Long> teacherIds = Lists.newArrayList();
        List<TeacherCls> teacherClss = teacherClsService.teacherClsList(0, companieClsId);
        teacherClss.stream().forEach(tc -> {
            teacherIds.add(tc.getBindTeacher());
        });
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.in(teacherIds));
        List<CompanyTeacher> companyTeachers = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue())
                .fetch();
        return companyTeachers;

    }

    public List<CompanyTeacher> screen(CompanyTeacherListVO companyTeacherVO) throws Exception {
        long userId = Long.parseLong(VerificationUtil.getUserId());
        if (companyTeacherVO.getCompanyClsId() != 0) {
            return findByCompanieClsId(companyTeacherVO.getCompanyClsId());
        } else if (companyTeacherVO.getCompanyDepartmentId() != 0) {
            return findByCompanyDepartmentId(companyTeacherVO.getCompanyDepartmentId());
        } else if (companyTeacherVO.getCompanyId() != 0) {
            return findByCompanieId(companyTeacherVO.getCompanyId());
        }
        return findByUserId(userId);
    }


    public long countTeacherList(PageEnty<CompanyTeacherListVO> companyTeacherListVO) throws Exception {
        CompanyTeacherListVO companyTeacherVO = companyTeacherListVO.getVo();
//        List<CompanyTeacherListDTO> recycleDTOS = Lists.newArrayList();
        List<CompanyTeacher> companyTeachers = screen(companyTeacherVO);
        if (!StringUtils.isEmpty(companyTeacherVO.getGender()))
            companyTeachers = companyTeachers.stream().filter(ct -> ct.getGender().equals(GenderType.map.get(companyTeacherVO.getGender()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(companyTeacherVO.getTeacherType()))
            companyTeachers = companyTeachers.stream().filter(ct -> ct.getTeacherType().equals(TeacherType.map.get(companyTeacherVO.getTeacherType()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(companyTeacherVO.getStatus()))
            companyTeachers = companyTeachers.stream().filter(ct -> ct.getStatus().equals(Status.map.get(companyTeacherVO.getStatus()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(companyTeacherVO.getTeacherName()))
            companyTeachers = companyTeachers.stream().filter(ct -> RegularUtils.matchStringByIndexOf(ct.getTeacherName(), companyTeacherVO.getTeacherName())).collect(Collectors.toList());
        int startPage = (companyTeacherListVO.getPage() - 1) * companyTeacherListVO.getNum();
        int endPage = companyTeacherListVO.getPage() * companyTeacherListVO.getNum() > companyTeachers.size() ? companyTeachers.size() : companyTeacherListVO.getPage() * companyTeacherListVO.getNum();
        companyTeachers.subList(startPage, endPage);
        return companyTeachers.size();
    }


    public List<CompanyTeacherListDTO> pageTeacherList(PageEnty<CompanyTeacherListVO> companyTeacherListVO) throws
            Exception {
        CompanyTeacherListVO companyTeacherVO = companyTeacherListVO.getVo();
        List<CompanyTeacherListDTO> recycleDTOS = Lists.newArrayList();
        List<CompanyTeacher> companyTeachers = screen(companyTeacherVO);
        if (!StringUtils.isEmpty(companyTeacherVO.getGender()))
            companyTeachers = companyTeachers.stream().filter(ct -> ct.getGender().equals(GenderType.map.get(companyTeacherVO.getGender()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(companyTeacherVO.getTeacherType()))
            companyTeachers = companyTeachers.stream().filter(ct -> ct.getTeacherType().equals(TeacherType.map.get(companyTeacherVO.getTeacherType()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(companyTeacherVO.getStatus()))
            companyTeachers = companyTeachers.stream().filter(ct -> ct.getStatus().equals(Status.map.get(companyTeacherVO.getStatus()))).collect(Collectors.toList());
        if (!StringUtils.isEmpty(companyTeacherVO.getTeacherName()))
            companyTeachers = companyTeachers.stream().filter(ct -> RegularUtils.matchStringByIndexOf(ct.getTeacherName(), companyTeacherVO.getTeacherName())).collect(Collectors.toList());
        int startPage = (companyTeacherListVO.getPage() - 1) * companyTeacherListVO.getNum();
        int endPage = companyTeacherListVO.getPage() * companyTeacherListVO.getNum() > companyTeachers.size() ? companyTeachers.size() : companyTeacherListVO.getPage() * companyTeacherListVO.getNum();
        companyTeachers.subList(startPage, endPage);
        companyTeachers.sort(Comparator.comparing(CompanyTeacher::getCreateTime).reversed());
        companyTeachers.stream().forEach(ct -> {
            CompanyTeacherListDTO companyTeacherListDTO = CopyUtil.copyEnty(ct, CompanyTeacherListDTO.class);
            companyTeacherListDTO.setIcon(ct.getIcon());
            companyTeacherListDTO.setTeacherName(ct.getTeacherName());
            companyTeacherListDTO.setTeacherPhone(ct.getTeacherPhone());
            companyTeacherListDTO.setCompanyId(ct.getCompanyId());
            CompanyDTO companyDTO = companyService.findByCompanyId(ct.getCompanyId());
            companyTeacherListDTO.setCompanyName(companyDTO.getCompanyName());
            companyTeacherListDTO.setTeacherType(ct.getTeacherType());
            companyTeacherListDTO.setGender(ct.getGender());
            companyTeacherListDTO.setEducationId(ct.getEducation());
            companyTeacherListDTO.setStatus(ct.getStatus());
            if (companyTeacherListDTO.getTeacherType().equals(TeacherType.TEACHER)) {
                List<TeacherCls> teacherCls = teacherClsService.teacherClsList(ct.getId(), 0);
                List<BinTeacherClsDTO> binTeacherCls = Lists.newArrayList();
                teacherCls.stream().forEach(tc -> {
                    BinTeacherClsDTO binTeacherClsDTO = new BinTeacherClsDTO();
                    CompanyClsDTO companyClsDTO = companyClsService.findByCompanyClsId(tc.getBindCls());
                    CompanyDepartmentDTO companyDepartmentDTO = companyDepartmentService.findByCompanyDepartmentId(companyClsDTO.getDepartmentId());
                    if (companyClsDTO != null) {
                        binTeacherClsDTO.setClsName(companyClsDTO.getClsName());
                        binTeacherClsDTO.setDepartmentName(companyDepartmentDTO.getGradeName());
                        if (tc.getIdentity() != null)
                            binTeacherClsDTO.setRelationship(tc.getIdentity().name);
                        binTeacherCls.add(binTeacherClsDTO);
                    }
                });
                companyTeacherListDTO.setBinTeacherCls(binTeacherCls);
            }
            recycleDTOS.add(companyTeacherListDTO);
        });
        return recycleDTOS;

    }

}

